package demo.view;

import util.MLog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;

public class MDrawable extends BitmapDrawable {
	private Rect bgRect;
	private float textSize;
	private float minTextSize = 2;
	private String text;

	public MDrawable(Resources res, Bitmap b, String text, float textSize) {
		super(res, b);
		this.textSize = textSize;
		this.text = text;
		float height = textSize + 10;
		float width = 180;
		bgRect = new Rect(0, 0, (int) width, (int) height);
		setBounds(bgRect);
	}

	@Override
	public void draw(Canvas canvas) {
		canvas.save();
		Rect rect = bgRect;
		int width = rect.right - rect.left;
		int height = rect.bottom - rect.top;

		Paint paint = getPaint();
		paint.setColor(Color.parseColor("#7bbedf"));
		canvas.drawRoundRect(new RectF(bgRect), 3, 3, paint);

		paint.setColor(Color.WHITE);
		//paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.ITALIC));
		paint.setTextSize(textSize > minTextSize ? textSize : minTextSize);
		canvas.drawText(text, 10, height * 3 / 4, paint);

		Bitmap b = getBitmap();
		int newBH = (int) (1.0f * height / 2);
		int newBW = (int) (1.0f * newBH * b.getWidth() / b.getHeight());
		Rect r = new Rect(width - 10 - newBW, (height - newBH) / 2, width - 10,
				(height + newBH) / 2);
		RectF dst = new RectF(r);
		canvas.drawBitmap(b, null, dst, paint);

		canvas.restore();
		MLog.i(getClass(), "textSize=" + textSize + "," + rect);
	}

}
